package com.wxh.multithread;

/**
 * @ClassName: TestPriority
 * @Description: 测试线程优先级
 * @Author wxh
 * @Date: 2022/1/6 10:17
 * @Version V1.0.0
 * @Since 1.8
 */
public class TestPriority {
    /**
     *  设置线程的优先级，让其尽可能CPU调度
     * @param args
     */
    public static void main(String[] args) {
        // 主线程,默认优先级
        System.out.println(Thread.currentThread().getName()+ "-->" + Thread.currentThread().getPriority());
        MyPriority priority = new MyPriority();

        Thread t1 = new Thread(priority);
        Thread t2 = new Thread(priority);
        Thread t3 = new Thread(priority);
        Thread t4 = new Thread(priority);
        Thread t5 = new Thread(priority);
        Thread t6 = new Thread(priority);

        // 先设置优先级，再启动
        t1.start();

        t2.setPriority(1);
        t2.start();

        t3.setPriority(4);
        t3.start();

        t4.setPriority(Thread.MAX_PRIORITY);
        t4.start();

        t5.setPriority(8);
        t5.start();

        t6.setPriority(7);
        t6.start();
    }
}


class MyPriority implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+ "-->" + Thread.currentThread().getPriority());
    }
}